package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.HouseDetailActivity;
import com.sunday.android.fangdaifu.bukong.entity.House;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/11/6.
 */

public class HouseListAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<House> dataSet;

    public HouseListAdapter(Context context, List<House> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_house_item, null);
        RecyclerView.LayoutParams params=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            House item=dataSet.get(position);
        ListHolder listHolder= (ListHolder) holder;
        listHolder.houseName.setText(item.getHouseName());
        listHolder.address.setText(item.getAddress());
        listHolder.equipNums.setText(String.format("沉降数%1d  倾斜数%2d  裂锋数%3d",item.getSettlement(),
                item.getHorizontal(),item.getCrack()));
        listHolder.equipWarningNums.setText(String.format("预警数%1d  损坏数%2d ",item.getSettlement(),
                item.getWarning(),item.getDamage()));

        listHolder.ll_house.setTag(item);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @Bind(R.id.house_name)
        TextView houseName;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.equip_nums)
        TextView equipNums;
        @Bind(R.id.equip_warning_nums)
        TextView equipWarningNums;
        @Bind(R.id.rl_house)
        LinearLayout  ll_house;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            ll_house.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            House house= (House) v.getTag();
            Intent intent=new Intent(mContext, HouseDetailActivity.class);
            intent.putExtra("houseId",house.getHouseId());
            intent.putExtra("houseName",house.getHouseName());
            mContext.startActivity(intent);
        }
    }

}
